package py.patch.match;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import py.patch.parse.SvnPatch;
import py.patch.util.DateBean;
import py.patch.util.PrintUtil;
import py.patch.util.ReadUtil;
import py.patch.util.SortUtil;

public class FileMatchByTime {
	public static Logger logger = Logger.getLogger(FileMatchByTime.class);
	private String dir;
	private String [] fileSuffix;
	
	private String startTime; //"yyyy-MM-dd HH:mm:ss"
	private String endTime;//"yyyy-MM-dd HH:mm:ss"

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String[] getFileSuffix() {
		return fileSuffix;
	}

	public void setFileSuffix(String[] fileSuffix) {
		this.fileSuffix = fileSuffix;
	}
	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 * @param startTime
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 * @param endTime
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
	public FileMatchByTime() {
	}

	public FileMatchByTime(String dir, String[] fileSuffix) {
		super();
		this.dir = dir;
		this.fileSuffix = fileSuffix;
	}

	public FileMatchByTime(String dir, String[] fileSuffix, String startTime,
			String endTime) {
		super();
		this.dir = dir;
		this.fileSuffix = fileSuffix;
		this.startTime = startTime;
		this.endTime = endTime;
	}

	public Map<String,Object> qryFileListByModfiytime(){
		Map<String,Object> newMap = new HashMap<String,Object>();
		
		logger.debug("*****************************read start*************************************");
		Map<String,Object> map = ReadUtil.getFileMap(new File(this.getDir()), null);
		logger.debug("*****************************read end*************************************");
		
		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			
			File file =(File) map.get(key);
			long fileLong = file.lastModified();
			long start = 0;
			long end = Long.MAX_VALUE;
			if(StringUtils.isNotEmpty(this.getStartTime())){
				start = DateBean.getLongFromDateString(this.getStartTime(), DateBean.FULL_FROMART_PATTERN);	
			}
			if(StringUtils.isNotEmpty(this.getStartTime())){
				end = DateBean.getLongFromDateString(this.getEndTime(), DateBean.FULL_FROMART_PATTERN);	
			}
			
			if( this.getFileSuffix() == null || Arrays.toString(this.getFileSuffix()).contains("[*]")){
				if(fileLong>=start && fileLong<=end){
					newMap.put(key, file.getAbsolutePath());
				}
			}else{
				String[] arr = this.getFileSuffix();
				for (String a : arr) {
					if(file.getName().endsWith(a)){
						if(fileLong>=start && fileLong<=end){
							newMap.put(key, file.getAbsolutePath());
							break;
						}
					}
				}
				
			}
		}
		
		return SortUtil.sortMapByKey(newMap);
	}
	
}
